﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;

namespace ZhongKeSite.Common
{
    public static partial class ExtHelper
    {
        #region 是否为邮箱地址
        /// <summary>
        /// 是否为邮箱地址
        /// </summary>
        /// <returns></returns>
        public static bool IsEmail(this string str)
        {
            Regex re = new Regex(@"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
            return re.IsMatch(str);
        }
        #endregion

        #region 是否为手机号码
        /// <summary>
        /// 是否为手机号码
        /// </summary>
        /// <returns></returns>
        public static bool IsMobile(this string str)
        {
            Regex re = new Regex(@"^1[123456789]\d{9}$");
            return re.IsMatch(str);
        }
        #endregion

        #region 中间星号截取
        /// <summary>
        /// 取
        /// </summary>
        /// <param name="FLength">前字符</param>
        /// <param name="BLength">后字符</param>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetHideStr3(this string str, int FLength, int BLength)
        {
            if (!string.IsNullOrEmpty(str))
            {
                if (str.Length >= FLength + BLength)
                {
                    int len = str.Length;
                    string strStart = str.Substring(0, FLength);
                    string strEnd = str.Substring(len - BLength, BLength);
                    return strStart + "****" + strEnd;
                }
                else
                {
                    return str;
                }
            }
            else
            {
                return "";
            }
        }
        #endregion

        #region  得到json对象安全键值
        /// <summary>
        ///  得到json对象安全键值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="jObject"></param>
        /// <param name="JKey"></param>
        /// <returns></returns>
        public static T GetJObectValue<T>(this JObject jObject, string JKey)
        {
            if (jObject.ContainsKey(JKey))
            {
                var type = typeof(T);
                var TValue = jObject.Value<T>(JKey);
                if (type == typeof(string))
                {
                    var SafetyValue = AntiSqlInject.Instance.GetSafetySql(jObject.Value<string>(JKey));
                    return (T)Convert.ChangeType(SafetyValue, typeof(T)); ;
                }
                return TValue;
            }
            else
            {
                return default(T);
            }
        }
        #endregion


        public static JObject ToJObject(this string JsonString)
        {
            JObject jObject = new JObject();
            try
            {
                if (!string.IsNullOrEmpty(JsonString))
                {
                    jObject = JObject.Parse(JsonString);
                }
                return jObject;
            }
            catch
            {
                return jObject;
            }
        }

        public static JArray ToJArray(this string JsonString)
        {
            JArray jArray = new JArray();
            try
            {
                if (!string.IsNullOrEmpty(JsonString))
                {
                    jArray = JArray.Parse(JsonString);
                }
                return jArray;
            }
            catch
            {
                return jArray;
            }
        }

        /// <summary>
        /// 文件路径转Base64
        /// </summary>
        /// <param name="FilePath">文件物理路径</param>
        /// <returns></returns>
        public static string ToBase64(this string FilePath)
        {
            string Base64 = string.Empty;
            if (!string.IsNullOrEmpty(FilePath))
            {
                byte[] byteArray = File.ReadAllBytes(FilePath);
                Base64 = Convert.ToBase64String(byteArray);
            }
            return Base64;
        }

        /// <summary>
        /// Base64字符编码
        /// </summary>
        /// <param name="FilePath">文件物理路径</param>
        /// <returns></returns>
        public static string ToBase64Encode(this string FilePath)
        {
            string Base64 = string.Empty;
            if (!string.IsNullOrEmpty(FilePath))
            {
                byte[] byteArray = File.ReadAllBytes(FilePath);
                Base64 = Convert.ToBase64String(byteArray);
                Base64 = System.Web.HttpUtility.UrlEncode(Base64);
            }
            return Base64;
        }

        /// <summary>
        /// Url编码
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static string ToEncode(this string Value)
        {
            string EncodeValue = string.Empty;
            if (!string.IsNullOrEmpty(Value))
            {
                EncodeValue = System.Web.HttpUtility.UrlEncode(Value);
            }
            return EncodeValue;
        }

        /// <summary>
        /// URL解码
        /// </summary>
        /// <param name="Value"></param>
        /// <returns></returns>
        public static string ToDecode(this string Value)
        {
            string EncodeValue = string.Empty;
            if (!string.IsNullOrEmpty(Value))
            {
                EncodeValue = System.Web.HttpUtility.UrlDecode(Value);
            }
            return EncodeValue;
        }

        /// <summary>
        /// byte转base64值
        /// </summary>
        /// <param name="Data"></param>
        /// <returns></returns>
        public static string ToBase64(this byte[] Data)
        {
            string Base64 = string.Empty;
            if (Data != null && Data.Length > 0)
            {
                Base64 = Convert.ToBase64String(Data);
            }
            return Base64;
        }
    }
}
